package com.mycompany.client;

import com.google.inject.Inject;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;

/**
 *
 * @author artyukhov
 */
public class LoginFrame extends JFrame
{
    private static final String BUTTON_NAME = "Connect";
    @Inject
    private ConnectedClientFactory factory;
    private final JTextArea hostArea = new JTextArea("localhost");
    private final JTextArea portArea = new JTextArea("4444");
    private final JTextArea nameArea = new JTextArea("your name");
    private ConnectedClient connectedClient;


    public LoginFrame()
    {
        super("Type connection data");
        Dimension labelSize = new Dimension(50, 20);
        JPanel connectionDataPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JLabel hostLabel = new JLabel("Host:");
        hostLabel.setPreferredSize(labelSize);

        hostArea.setPreferredSize(new Dimension(100, 20));
        JLabel portLabel = new JLabel("Port:");
        portLabel.setPreferredSize(labelSize);

        portArea.setPreferredSize(new Dimension(50, 20));

        connectionDataPanel.add(hostLabel);
        connectionDataPanel.add(hostArea);
        connectionDataPanel.add(portLabel);
        connectionDataPanel.add(portArea);

        JPanel inputNamePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JLabel nameLabel = new JLabel("Your Name:");
        nameLabel.setPreferredSize(labelSize);

        nameArea.setPreferredSize(new Dimension(100, 20));
        inputNamePanel.add(nameLabel);
        inputNamePanel.add(nameArea);

        JButton submit = new JButton(BUTTON_NAME);
        submit.addActionListener(new ButtonListener());
        getContentPane().add(connectionDataPanel, BorderLayout.NORTH);
        getContentPane().add(inputNamePanel, BorderLayout.CENTER);
        getContentPane().add(submit, BorderLayout.SOUTH);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        setSize(300, 200);
    }


    public ConnectedClient getLoginObject() throws InterruptedException
    {
        synchronized (this) {
            this.wait();
        }
        return connectedClient;
    }

    private class ButtonListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            

            String hostStr = hostArea.getText();
            String portStr = portArea.getText();
            String name = nameArea.getText();
            try {
                connectedClient = factory.create(hostStr, portStr, name);
                synchronized (LoginFrame.this) {
                    LoginFrame.this.notify();
                }
            } catch (RuntimeException exc) {
                JOptionPane.showMessageDialog(
                        null, "Error " + exc.getMessage(), "Connection Error", JOptionPane.ERROR_MESSAGE);
            }
        }
    }
}
